"""任务提示词模板."""

from __future__ import annotations

from langchain_core.prompts import PromptTemplate

from .tasks_loader import load_task_template


def build_risk_control_prompt() -> PromptTemplate:
    template = load_task_template("risk_control_officer_task")
    prompt_text = (
        "{task_description}\n\n"
        "输入信息：\n"
        "- 当前时间：{current_time}\n"
        "- 目标 ETF 代码：{fund}\n"
        "- 历史数据摘要：{history_summary}\n"
        "- 市场搜索结果（JSON 列表，含来源）：{web_sources}\n"
        "- 定量风险指标（JSON，含波动率、回撤、流动性、VaR）：{quant_metrics}\n\n"
        "请严格按照任务步骤生成风险控制报告：\n"
        f"{template.expected_output or ''}\n"
        "生成报告时需明确标注所有数据来源与工具调用信息。"
    )
    return PromptTemplate(
        input_variables=["current_time", "fund", "history_summary", "web_sources", "quant_metrics", "task_description"],
        template=prompt_text,
    )


def build_quant_analysis_prompt() -> PromptTemplate:
    template = load_task_template("quant_analysis_task")
    prompt_text = (
        "{task_description}\n\n"
        "上下文：\n"
        "- 当前时间：{current_time}\n"
        "- 目标 ETF 代码：{fund}\n"
        "- 历史数据说明：{history_summary}\n"
        "- 特征工程说明：{feature_summary}\n"
        "- 风险控制要点：{risk_summary}\n"
        "{quant_result_summary_section}\n"
        "确保所有参数和随机种子与任务要求一致，并根据以下格式输出：\n"
        f"{template.expected_output or ''}"
    )
    return PromptTemplate(
        input_variables=[
            "current_time",
            "fund",
            "history_summary",
            "feature_summary",
            "risk_summary",
            "task_description",
            "quant_result_summary",
        ],
        template=prompt_text,
        partial_variables={
            "quant_result_summary_section": (
                "- 量化分析结果（模型训练、回测、评估指标）：\n{quant_result_summary}\n"
                if "{quant_result_summary}" in prompt_text
                else ""
            ),
        },
    )


def build_prediction_prompt() -> PromptTemplate:
    template = load_task_template("prediction_task")
    prompt_text = (
        "{task_description}\n\n"
        "背景数据：\n"
        "- 当前时间：{current_time}\n"
        "- 目标 ETF 代码：{fund}\n"
        "- 当前持仓：{position}\n"
        "- 最新行情：{latest_quote}\n"
        "- 量化分析摘要（含最佳模型与关键指标）：{quant_summary}\n"
        "- 风险控制要点：{risk_summary}\n"
        "- 技术指标现状：{technical_snapshot}\n"
        "{model_prediction_section}\n"
        "请基于上述信息生成未来 5-10 日预测报告，输出格式需满足：\n"
        f"{template.expected_output or ''}"
    )
    return PromptTemplate(
        input_variables=[
            "current_time",
            "fund",
            "position",
            "latest_quote",
            "quant_summary",
            "risk_summary",
            "technical_snapshot",
            "task_description",
            "model_prediction",
        ],
        template=prompt_text,
        partial_variables={
            "model_prediction_section": (
                "- 模型预测结果：\n{model_prediction}\n"
                if "{model_prediction}" in prompt_text
                else ""
            ),
        },
    )

